/* ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
   Module Name          : com.yaodian100.ec.supplierapply.remote.RemoteSupplierServiceImpl
   Module Description   :

   Date Created      : 2009/12/2
   Original Author   : andy.chuang
   Team              : yaodian100
   ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
   MODIFICATION HISTORY
   ------------------------------------------------------------------------------
   Date Modified       Modified by       Comments
   ------------------------------------------------------------------------------
   ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
package com.yaodian100.ec.supplierapply.remote;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import com.yaodian100.core.common.service.BaseManager;
import com.yaodian100.core.common.utils.StringUtils;
import com.yaodian100.ec.supplierapply.entity.SupplierApply;

/**
 * @author andy.chuang
 * 
 */
public class RemoteSupplierApplyServiceImpl implements RemoteSupplierApplyService {
	private static Logger LOGGER = LoggerFactory.getLogger(RemoteSupplierApplyServiceImpl.class);
	// @Autowired
	// private TemplateService velocityService;
	// @Autowired
	// private UserService userService;
	// @Autowired
	// private MailService mailService;
	@Autowired
	private BaseManager baseMgr;

	// @Autowired
	// private GroupService groupService;
	// @Autowired
	// private RoleService roleService;

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.yaodian100.ec.supplierapply.remote.RemoteSupplierApplyService#get(java.lang.String)
	 */
	@Override
	public SupplierApply get(String supplierID) {
		return (SupplierApply) baseMgr.get(SupplierApply.class, supplierID);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.yaodian100.ec.supplierapply.remote.RemoteSupplierApplyService#updateSupplier(java.lang.String,
	 * java.lang.Integer, java.lang.String, java.lang.String, java.lang.String, java.lang.Integer)
	 */
	@Override
	public String updateSupplier(String supplierID, Integer approveStatus, String bankAccountName, String bankAccountNum,
			String bankName, Integer status, String paymentBankArea) {

		try {
			SupplierApply sa = (SupplierApply) baseMgr.get(SupplierApply.class, supplierID);

			// if (sa.getApproveStatus() == 1 && approveStatus == 2) {
			// User[] users = createUser(sa);
			// sendMail(sa, users);
			// }
			if (sa.getApproveStatus() != 2) {// 已是財務已核可的就不能再update
				sa.setApproveStatus(approveStatus);
			}
			sa.setBankAccountName(bankAccountName);
			sa.setBankAccountNum(bankAccountNum);
			sa.setBankName(bankName);
			sa.setStatus(status);
			if (StringUtils.isNotBlank(paymentBankArea)) {
				sa.setPaymentBankArea(paymentBankArea);
			}
			baseMgr.update(sa);
			return "true";
			// else {
			// return "sa.getApproveStatus()==2";
			// }
		} catch (Exception e) {
			e.printStackTrace();
			LOGGER.error(e.getMessage());
			LOGGER.error("updateSupplier failed!!");
			return e.toString();

		}

	}
	//
	// private String createPassword() {
	// String str = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890";
	// int size = str.length();
	// StringBuffer sb = new StringBuffer();
	// Random ran = new Random();
	// for (int i = 0; i < 8; i++) {
	// int start = ran.nextInt(size);
	// String tmp = str.substring(start, start + 1);
	// sb.append(tmp);
	// }
	// String password = sb.toString();
	// return password;
	// }
	//
	// private User[] createUser(SupplierApply ts) throws CoreException {
	// Group group = new Group("S." + ts.getSupplierID(), ts.getSupplierID());
	// groupService.save(group);
	// // PM
	// User user = new User(ts.getSupplierID() + "PM");
	// String password = createPassword();
	// user.setPassword(password);
	// user.setGroup(group);
	// user.setStatus(User.STATUS_ACTIVE);
	// try {
	// user.getRoles().add(roleService.get(Role.Default.SP_PM.name(), Role.Type.OTHER));
	// user.getRoles().add(roleService.get(Role.Default.SP.name(), Role.Type.OTHER));
	// userService.createUser(user, user.getUsername(), group.getCode());
	// } catch (CoreException e) {
	// LOGGER.error("create user for " + ts.getSupplierID() + " failed!");
	// throw new RuntimeException(e);
	// }
	// // FM
	// User userFM = new User(ts.getSupplierID() + "FIN");
	// String passwordFM = createPassword();
	// userFM.setPassword(passwordFM);
	// userFM.setGroup(group);
	// userFM.setStatus(User.STATUS_ACTIVE);
	// try {
	// userFM.getRoles().add(roleService.get(Role.Default.SP_FM.name(), Role.Type.OTHER));
	// userFM.getRoles().add(roleService.get(Role.Default.SP.name(), Role.Type.OTHER));
	// userService.createUser(userFM, userFM.getUsername(), group.getCode());
	// } catch (CoreException e) {
	// LOGGER.error("create user for " + ts.getSupplierID() + " failed!");
	// throw new RuntimeException(e);
	// }
	// User[] users = new User[2];
	// users[0] = user;
	// users[1] = userFM;
	// return users;
	// }
	//
	// private void sendMail(SupplierApply ts, User[] users) {
	// Map<String, String> map = new HashMap<String, String>();
	// map.put("contantName", ts.getContactName());
	// map.put("pmUser", users[0].getUsername());
	// map.put("pmUserPassword", users[0].getPassword());
	// map.put("fmUser", users[1].getUsername());
	// map.put("fmUserPassword", users[1].getPassword());
	// map.put("contactName", ts.getContactName());
	// try {
	// String body = velocityService.format("supplier_createUser.vm", map);
	// Mail mail = new Mail("create user for upplier", body, ts.getContactEmail(), null);
	// mailService.save(mail);
	// } catch (CoreException e) {
	// LOGGER.error("send mail for " + ts.getSupplierID() + " failed!!");
	// throw new RuntimeException(e);
	// }
	// }

}
